import com.powerfl.frm.taf.*;

final public function stateGet( $state:String ):*{
	if( tdata( '@STATEVAR' )[$state] === undefined ){
		error( '상태가 존재하지 않습니다. - ' + $state );
	}else{
		return tdata( '@STATEVAR' )[$state];
	}
}
final public function stateInit( ...$states ):void{
	var i:int, j:int, temp:*, state:String;
	for( i = 0, j = $states.length ; i < j ; ++i ){
		state = $states[i];
		temp = tdata( '@STATE' )[state];
		if( temp === undefined ){
			error( '상태가 존재하지 않습니다. - ' + state );
		}else{
			tdata( '@STATEVAR' )[state] = temp[0];
		}
	}
}
final public function stateSelect( ...$state_index ):void{
	var i:int, j:int, temp:*, state:String, val:*;
	for( i = 0, j = $state_index.length ; i < j ; i += 2 ){
		state = $state_index[i];
		val = $state_index[i + 1];
		temp = tdata( '@STATE' )[state]; 
		if( temp === undefined ){
			error( '상태가 존재하지 않습니다. - ' + state );
		}else if( temp.length - 1 < val ){
			error( '상태 인덱스가 존재하지 않습니다. - ' + val + ' : ' + state + ' : ' + temp );
		}else{
			tdata( '@STATEVAR' )[state] = temp[val];
		}
	}
}
final public function stateSet( ...$state_value ):void{
	var i:int, j:int, temp:*, state:String, val:*;
	for( i = 0, j = $state_value.length ; i < j ; i += 2 ){
		state = $state_value[i];
		val = $state_value[i + 1];
		temp = tdata( '@STATE' )[state]; 
		if( temp === undefined ){
			error( '상태가 존재하지 않습니다. - ' + state );
		}else if( temp.indexOf( val ) === -1 ){
			error( '상태 값이가 존재하지 않습니다. - ' + val + ' : ' + state + ' : ' + temp );
		}else{
			tdata( '@STATEVAR' )[state] = val;
		}
	}
}
final public function stateDown( $state:String, ...$flows ):Boolean{
	var i:int, j:int, temp:*;
	temp = tdata( '@STATE' )[$state]; 
	if( temp === undefined ){
		error( '상태가 존재하지 않습니다. - ' + $state );
	}
	i = temp.indexOf( tdata( '@STATEVAR' )[$state] );
	if( i > 0 ){
		tdata( '@STATEVAR' )[$state] = temp[i-1];
		return operatorConditional( true, $flows );
	}else{
		return operatorConditional( false, $flows );
	}
}
final public function stateUp( $state:String, ...$flows ):*{
	var i:int, j:int, temp:*, arg:Array;
	temp = tdata( '@STATE' )[$state]; 
	if( temp === undefined ){
		error( '상태가 존재하지 않습니다. - ' + $state );
	}
	i = temp.indexOf( tdata( '@STATEVAR' )[$state] );
	if( i < temp.length - 1 ){
		tdata( '@STATEVAR' )[$state] = temp[i+1];
		return operatorConditional( true, $flows );
	}else{
		return operatorConditional( false, $flows );
	}
}